package com.swedne.test.livedata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import com.swedne.test.MainActivity;
import com.swedne.test.R;

import static androidx.lifecycle.Lifecycle.State.DESTROYED;

/**
 * @author swedne
 */
public class LiveDataTestActivity extends AppCompatActivity {
    private volatile Integer i = new Integer(0);
    private TextView tvText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvText = findViewById(R.id.tv_text);
        System.out.println("onChanged=" + this.getClass().getSimpleName());
        LiveBus.getInstance().with(this.getClass().getSimpleName(), Integer.class).observe(this, integer -> System.out.println("onChanged=" + i));
        asyncAdd();
        startActivity(new Intent(this, MainActivity.class));

        getLifecycle().addObserver(new LifecycleObserverDemo() {
            @Override
            public void onDestroy() {
                super.onDestroy();
            }
        });
    }

    private void asyncAdd() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                LiveBus.getInstance().post(LiveDataTestActivity.this.getClass().getSimpleName(), i);
            }
        }).start();
    }
}
